/* Any Bug U Find In This Code Contact Author
*
*
* This Header File Is Used In Huffman Compression file
*         -Read Input File And Create Table
*	      -Table Contains Information About Charcter In Input File
*         -Store Output File(Store Header and Output Bits In Byte)
*
*  Author : Vishwajeet C. Dusane(dusanevishwajeet@yahoo.co.in)
*/



class FileReaderWriter
{
   //variables
   private:
		FILE *fin,*fout;
 //methods
   private:

 //variables
   public:


 //methods
   public:
	   int   ratio(char [15],char [15]);
	   void  readFileChar(char [15],struct list **);
	   void  openForWrite(char [15]);

	   void  openForRead(char [15]);
	   char  readInfile();
       int   readInfile();
       char* readInfile(int );


	   void  writeIntoOutFile(char *,int )
};

void FileReaderWriter::ReadFileChar(char fnmR[15],struct list **head_list)
{

 //============================================================================
  ch=(char *)malloc(1);
  if((fpR=open(fnmR,O_RDONLY))==-1)
  {
	printf(" \n %s File not found",fnmR);
	exit(0);
  }
  //===========================================================================
  //Start To Read Input File
  else
  {
  //===========================================================================
  //Read Input File Character by Character And Create List of character
	while((read(fpR,ch,1))!=0)
	{
	   make_list(&temp,ch);
	   file_size=file_size+1;
	   head_list=temp;
	}
   //End of Read File and Create List
  }
}






//============================================================================
//This Method is Used To find Ratio of Files
int FileReaderWriter::ratio(char fnmR[15],char fnmW[15])
{
	struct stat statbuf;
	long orgfilesize;
	int fileratio;
	//Status of original file
	stat(fnmR,&statbuf);
	printf("\nStatus of file ''%s''",fnmR);
	printf("\nSize of file in bytes %ld",statbuf.st_size);
	printf("\nLast file accessed time and date %s",ctime(&statbuf.st_ctime));
	orgfilesize=statbuf.st_size;
	//Status of compressed file
	stat(fnmW,&statbuf);
	printf("\nStatus of compressed file ''%s''",fnmR);
	printf("\nSize of file in bytes %ld",statbuf.st_size);
	printf("\nLast file accessed time and date %s",ctime(&statbuf.st_ctime));
	fileratio=(statbuf.st_size*100)/orgfilesize;
	printf("\nCompression ratio is %d%\n",fileratio);

}

//============================================================================
//This Method Is Used For Opening New File In Read Mode
void FileReaderWrite::openForRead(char fnmR[15])
{
  if((fin=open(fnmR,O_RDONLY))==-1)
  {
	printf("\n %s File not found",fnmR);
	fcloseall();
	exit(0);
  }

}
//============================================================================
//This Method Is Used to Read Character From i/p File
//Returns Character From File
char FileReaderWriter::readInfile()
{
  char ch;
	 read(fin,ch,1);
  return ch;

}
//============================================================================
//This Method Is Used to Read Character From i/p File
//Returns Character From File
int FileReaderWriter::readInfile()
{
  char ch;
    return read(fin,ch,1);

}

//============================================================================
//This Method Is Used to Read Character From i/p File
//Returns Character From File
char* FileReaderWriter::readInfile(int no)
{
  char *ch;
         read(fin,ch,no);
    return ch;

}

//============================================================================
//This Method Is To Write Character In Byte
//Returns Number Of Written Into File
void FileReaderWrite::writeIntoOutFile(char *char_write,int no)
{
	 return write(fout,char_write,no);

}

//============================================================================
//This Method Open File In write mode

void FileReaderWrite::openForWrite(char fnmW[15])
{

  if((fout=open(fnmW,O_WRONLY|O_CREAT|O_TRUNC,S_IWRITE|S_IREAD))==-1)
   {
	printf("\n %s File Already Exist OR Enable to create new file",fnmW);
	fcloseall();
	exit(0);
   }
}